﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using System.Diagnostics;


namespace MediaDataModel.Common.Other
{
    public class Logger
    {
        private static FileStream logfile;
        private static TextWriterTraceListener traceListener;
        private static int LogLevel = 0;
        private static string currentLogContent;
        public static List<string> lastLog = new List<string>();

        public static void InitLogger()
        {
            try
            {
                string logdir = ConfigurationManager.AppSettings["LogFolder"];
                if (string.IsNullOrEmpty(logdir) == true)
                    logdir = "logs";
                LogLevel = int.Parse(ConfigurationManager.AppSettings["LogLevel"] == null ? "0" : ConfigurationManager.AppSettings["LogLevel"]);
                if (!System.IO.Directory.Exists(logdir))
                {
                    System.IO.Directory.CreateDirectory(logdir);
                }
                string logPath = Path.Combine(logdir, "Log_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");

                if (!File.Exists(logPath))
                {
                    logfile = new FileStream(logPath, System.IO.FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
                }
                else
                {
                    logfile = new FileStream(logPath, System.IO.FileMode.Append,FileAccess.Write, FileShare.Read);
                }

                StreamWriter sw = new StreamWriter(logfile,Encoding.UTF8);
                traceListener = new TextWriterTraceListener(sw);
                
                Trace.Listeners.Add(traceListener);
            }
            catch (Exception exp)
            {
                //MessageBox.Show("初始化Logger异常！" + exp.StackTrace);
                throw exp;
            }
        }

        public static void Write(string logcontent)
        {
            currentLogContent = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + logcontent;
            traceListener.WriteLine(currentLogContent);
            traceListener.Flush();
            
            lock (lastLog)
            {
                if (lastLog.Count > 50)
                {
                    List<string> tempList = new List<string>();
                    for (int i = 50; i <= 0; i--)
                        tempList.Add(lastLog[lastLog.Count - i]);
                    lastLog.Clear();
                    lastLog.AddRange(tempList);
                }
            }
        }

        public static void Write(LogLevel level, string message)
        {
            if (level.GetHashCode() >= LogLevel)
            {
                Write("["+level.ToString()+"]"+message);
            }
        }

        public static void DeInitLogger()
        {
            traceListener.Close();
        }

        public static string GetCurrentLogContent()
        {
            return currentLogContent;
        }
    }

    public enum LogLevel
    {
        DEBUG = 0,
        NORMAL = 1,
        WARNING = 2,
        ERROR = 3,
        CRITICAL = 4
    }
}

